package com.tau.egonetwork.gui.core;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.tau.egonetwork.common.Log;
import com.tau.egonetwork.gui.Environment;

public class NumberInputDialog extends Dialog {

	private static final int CANCEL = -1;
	
	private Shell shell;
	private String messageText;
	
	private int minValue;
	private int maxValue;
	
	private int returnValue = CANCEL;
	
	public NumberInputDialog(Shell parent) {
		super(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
	}
	
	public void setMessageText(String text) {
		this.messageText = text;
	}
	
	public void setLimits(int min, int max) {
		this.minValue = min;
		this.maxValue = max;
	}
	
	public int open() {
		
		Log.writeLineToLog("NumberInputDialog's open() called with " + getText());
		
		shell = new Shell(getParent(), getStyle());
		shell.setText(getText());
		
		Log.writeLineToLog("createBaseWindowContents()");

		createBaseWindowContents();		
			
		shell.pack();
		
		//set shell at the center of display:
	    Monitor primary = getParent().getMonitor();
	    Rectangle bounds = primary.getBounds();
	    Rectangle rect = shell.getBounds();
	    int x = bounds.x + (bounds.width - rect.width) / 2;
	    int y = bounds.y + (bounds.height - rect.height) / 2;
	    shell.setLocation(x, y);
		
		Log.writeLineToLog("opening dialog");

		shell.open();
		
		//keep shell open, waiting for user input:
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		
		return returnValue;

	}
	
	private void createBaseWindowContents() {
		
		shell.setLayout(new GridLayout(1, true));
		shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
				
		Composite dialogComposite = new Composite(shell, SWT.NONE);
		dialogComposite.setLayout(new GridLayout(1, true));
		GridData dialogCompositeGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
		dialogCompositeGridData.widthHint = 240;
		dialogComposite.setLayoutData(dialogCompositeGridData);
		
		Label messageLabel = new Label(dialogComposite, SWT.NONE);
		messageLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false));
		messageLabel.setText(messageText);
		
		final Label alertLabel = new Label(dialogComposite, SWT.NONE);
		alertLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		alertLabel.setForeground(Environment.COLOR_RED);
		
		final Text inputText = new Text(dialogComposite, SWT.BORDER | SWT.FLAT);
		GridData inputTextGridData = new GridData(SWT.CENTER, SWT.CENTER, false, false);
		inputTextGridData.widthHint = 100;
		inputText.setLayoutData(inputTextGridData);
		inputText.setTextLimit(4);
		
		Composite buttonsComposite = new Composite(dialogComposite, SWT.NONE);
		buttonsComposite.setLayout(new GridLayout(2, false));
		buttonsComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
		
		final Button okButton = new Button(buttonsComposite, SWT.PUSH);
		okButton.setImage(Environment.ICON_OK);
		okButton.setText("OK");
		okButton.addListener(SWT.Selection, new Listener() {
			
			@Override
			public void handleEvent(Event arg0) {
				int val = Integer.parseInt(inputText.getText());
				if (val >= minValue && val <= maxValue) {
					returnValue = val;
					shell.close();
				} else {
					//value not legal for this operation - alert user:
					alertLabel.setText("Illegal value - must be between " + minValue + " and " + maxValue + "!");					
				}
			}
		});
		
		Button cancelButton = new Button(buttonsComposite, SWT.PUSH);
		cancelButton.setImage(Environment.ICON_ERROR);
		cancelButton.setText("Cancel");
		cancelButton.addListener(SWT.Selection, new Listener() {
			
			@Override
			public void handleEvent(Event arg0) {
				shell.close();
			}
		});
		
		inputText.addListener(SWT.Traverse, new Listener() {
			
			@Override
			public void handleEvent(Event event) {
				if (event.detail == SWT.TRAVERSE_RETURN)
					okButton.notifyListeners(SWT.Selection, new Event());
			}
		});
		
		//make sure only digits are entered to the text (and disable ok button when empty):
		inputText.addListener(SWT.KeyUp, new Listener() {
			
			@Override
			public void handleEvent(Event arg0) {				
				String text = "";
				if (inputText.getText() != null) {
					String originalText = inputText.getText();
					int len = originalText.length();
					for (int i = 0; i < len; i++) {
						char c = originalText.charAt(i);
						if (c >= '0' && c <= '9') {
							if (i == 0 && c == '0')
								continue;
							text += c;
						}
					}
				}
				inputText.setText(text);
				inputText.setSelection(new Point(text.length(), text.length()));
				okButton.setEnabled(text.length() > 0);
			}
		});

	}

}
